package com.taxicalculator.ui;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.taxicalculator.dao.ILocationSQLite;
import com.taxicalculator.dao.ITaxiCalculatorDAO;
import com.taxicalculator.dao.LocationsSQLite;
import com.taxicalculator.dao.TaxiCalculatorDAO;


public class MainActivity extends Activity implements LocationListener{
	
	// Thomas Maco added this to be able to use GPS and return current address
	private static final int CONVERT_MILSECONDS = 0;
	private double longitude;
	private double latitude;
	private int fromLocationType = 0;
	//these are hard coded since we do not have a preferences for user
	private int secondsGPSInterval = 0;
	private int meterGPSInterval = 0;
	ITaxiCalculatorDAO TaxiCalculator = new TaxiCalculatorDAO();
	ILocationSQLite locationSQLite = new LocationsSQLite(this);
	// makes GPS Available
	LocationManager locationManager;
	//for geoCoder thread
	AddressGEOCoder geoCoder = new AddressGEOCoder();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activitymain);

		// initialized Location manger
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
	}
	
	/**
	 * 
	 * @author Thomas Maco
	 */
	@Override
	public void onLocationChanged(Location location) {
		// gets Latitude and Longitude for the current location
		latitude = location.getLatitude();
		longitude = location.getLongitude();
		// String for a label to display current Address
		try {
			geoCoder.execute();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
	

	}

	@Override
	public void onProviderEnabled(String provider) {
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	 public void onClick(View v) {
         
         // make an intent that will display the results screen.
                         Intent faqActivityIntent = new Intent(this, FaqActivity.class);
                 // go to the new intent.
                 startActivity(faqActivityIntent);
                 
         }
	 public void calcClickMain(View v){
		 Intent CalcActivity = new Intent(this, CalcActivity.class);
		 startActivity(CalcActivity);
		 finish();
	 }
	 

		/**
		 * Method is called when App is paused
		 */
		@Override
		protected void onPause() {
			super.onPause();
			// turn off location updates
			if (locationManager != null) {
				locationManager.removeUpdates(this);
			}
		}

		/**
		 * Method is called when App is resumed
		 */
		@Override
		protected void onResume() {
			super.onResume();
			// turns back on location updates
			if (locationManager != null) {
				locationManager.requestLocationUpdates(
						locationManager.GPS_PROVIDER, secondsGPSInterval*CONVERT_MILSECONDS , meterGPSInterval, this);
			}
		}

		/**
		 * 
		 * @author Thomas Maco
		 *
		 */
		class AddressGEOCoder extends AsyncTask<TaxiCalculatorDAO, Integer, String> {

			/**
			 * @author Thomas Maco
			 * Background thread for when the network is need
			 */
			@Override
			protected String doInBackground(TaxiCalculatorDAO... params) {
				String UserAddress = null;
				try {
					UserAddress = TaxiCalculator.getAddress(latitude, longitude);
					locationSQLite.saveLocation(UserAddress, fromLocationType, latitude, longitude);
				} catch (Exception e) {
						e.printStackTrace();
				}
				return UserAddress;
			}
			
		}	 
}
